home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V1 CW9 / 68K and PPC Projects (CW9) / 4.2 - Updater / Updater.c < prev    next >
C/C++ Source or Header  |  1996-05-28  |  6KB  |  281 lines

  1. /********************************************************/
  2. /*                                                        */
  3. /*  Updater Code from Chapter Four of                     */
  4. /*                                                        */
  5. /*    ** The Macintosh C Programming Primer, 2nd Ed. **    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /*  This program demonstrates specific Mac programming    */
  10. /*    techniques.                                            */
  11. /*                                                        */    
  12. /********************************************************/
  13.  
  14. #include <limits.h>
  15.  
  16. #define kBaseResID            128
  17. #define    kMoveToFront        (WindowPtr)-1L
  18. #define kSleep                LONG_MAX
  19.  
  20. #define kScrollBarAdjust    (16-1)
  21. #define    kLeaveWhereItIs        false
  22. #define kNormalUpdates        true
  23.  
  24. #define kMinWindowHeight    50
  25. #define    kMinWindowWidth        80
  26.  
  27.  
  28. /*************/
  29. /*  Globals  */
  30. /*************/
  31.  
  32. Boolean        gDone;
  33.  
  34.  
  35. /***************/
  36. /*  Functions  */
  37. /***************/
  38.  
  39. void    ToolBoxInit( void );
  40. void    WindowInit( void );
  41. void    EventLoop( void );
  42. void    DoEvent( EventRecord *eventPtr );
  43. void    HandleMouseDown( EventRecord *eventPtr );
  44. void    DoUpdate( EventRecord *eventPtr );
  45. void    DoActivate( WindowPtr window, Boolean becomingActive );
  46. void    DoPicture( WindowPtr window, PicHandle picture );
  47. void    CenterPict( PicHandle picture, Rect *destRectPtr );
  48.  
  49.  
  50. /******************************** main *********/
  51.  
  52. void    main( void )
  53. {
  54.     ToolBoxInit();
  55.     WindowInit();
  56.     
  57.     EventLoop();
  58. }
  59.  
  60.  
  61. /*********************************** ToolBoxInit */
  62.  
  63. void    ToolBoxInit( void )
  64. {
  65.     InitGraf( &qd.thePort );
  66.     InitFonts();
  67.     InitWindows();
  68.     InitMenus();
  69.     TEInit();
  70.     InitDialogs( 0L );
  71.     InitCursor();
  72. }
  73.  
  74.  
  75. /******************************** WindowInit *********/
  76.  
  77. void    WindowInit( void )
  78. {
  79.     WindowPtr    window;
  80.     
  81.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  82.     
  83.     if ( window == nil )
  84.     {
  85.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  86.         ExitToShell();
  87.     }
  88.     
  89.     SetWRefCon ( window, (long)kBaseResID );
  90.     ShowWindow( window );
  91.     
  92.     window = GetNewWindow( kBaseResID+1, nil, kMoveToFront );
  93.     
  94.     if ( window == nil )
  95.     {
  96.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  97.         ExitToShell();
  98.     }
  99.     
  100.     SetWRefCon ( window, (long)( kBaseResID+1 ) );
  101.     ShowWindow( window );
  102. }
  103.  
  104.  
  105. /******************************** EventLoop *********/
  106.  
  107. void    EventLoop( void )
  108. {        
  109.     EventRecord        event;
  110.     
  111.     gDone = false;
  112.     while ( gDone == false )
  113.     {
  114.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  115.             DoEvent( &event );
  116.     }
  117. }
  118.  
  119.  
  120. /************************************* DoEvent     */
  121.  
  122. void    DoEvent( EventRecord *eventPtr )
  123. {
  124.     Boolean    becomingActive;
  125.     
  126.     switch ( eventPtr->what )
  127.     {
  128.         case mouseDown: 
  129.             HandleMouseDown( eventPtr );
  130.             break;
  131.         case updateEvt:
  132.             DoUpdate( eventPtr );
  133.             break;
  134.         case activateEvt:
  135.             becomingActive = ( (eventPtr->modifiers & activeFlag) == activeFlag );
  136.             
  137.             DoActivate( (WindowPtr)eventPtr->message, becomingActive );
  138.             break;
  139.     }
  140. }
  141.  
  142.  
  143. /************************************* HandleMouseDown */
  144.  
  145. void    HandleMouseDown( EventRecord *eventPtr )
  146. {
  147.     WindowPtr        window;
  148.     short            thePart;
  149.     GrafPtr            oldPort;
  150.     long            windSize;
  151.     Rect            growRect;
  152.     
  153.     thePart = FindWindow( eventPtr->where, &window );
  154.     
  155.     switch ( thePart )
  156.     {
  157.         case inSysWindow : 
  158.             SystemClick( eventPtr, window );
  159.             break;
  160.         case inContent:
  161.             SelectWindow( window );
  162.             break;
  163.         case inDrag : 
  164.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  165.             break;
  166.         case inGoAway :
  167.             if ( TrackGoAway( window, eventPtr->where ) )
  168.                 gDone = true;
  169.             break;
  170.         case inGrow:
  171.             growRect.top = kMinWindowHeight;
  172.             growRect.left = kMinWindowWidth;
  173.             growRect.bottom = SHRT_MAX;
  174.             growRect.right = SHRT_MAX;
  175.             windSize = GrowWindow( window, eventPtr->where, &growRect );
  176.             if ( windSize != 0 )
  177.             {
  178.                 GetPort( &oldPort );
  179.                 SetPort( window );
  180.                 EraseRect( &window->portRect );
  181.                 SizeWindow( window, LoWord( windSize ),
  182.                         HiWord( windSize ), kNormalUpdates );
  183.                 InvalRect( &window->portRect );
  184.                 SetPort( oldPort );
  185.             }
  186.             break;
  187.         case inZoomIn:
  188.         case inZoomOut:
  189.             if ( TrackBox( window, eventPtr->where, thePart ) )
  190.             {
  191.                 GetPort( &oldPort );
  192.                 SetPort( window );
  193.                 EraseRect( &window->portRect );
  194.                 ZoomWindow( window, thePart, kLeaveWhereItIs );
  195.                 InvalRect( &window->portRect );
  196.                 SetPort( oldPort );
  197.             }
  198.             break;
  199.     }
  200. }
  201.  
  202.  
  203. /************************************* DoUpdate     */
  204.  
  205. void    DoUpdate( EventRecord *eventPtr )
  206. {
  207.     short        pictureID;
  208.     PicHandle    picture;
  209.     WindowPtr    window;
  210.     
  211.     window = (WindowPtr)eventPtr->message;
  212.     
  213.     BeginUpdate( window );
  214.     pictureID = GetWRefCon ( window );
  215.     picture = GetPicture( pictureID );
  216.     
  217.     if ( picture == nil )
  218.     {
  219.         SysBeep( 10 );    /*  Couldn't load the PICT resource!!!  */
  220.         ExitToShell();
  221.     }
  222.     
  223.     DoPicture( window, picture );
  224.     EndUpdate( window );
  225. }
  226.  
  227.  
  228. /************************************* DoActivate     */
  229.  
  230. void    DoActivate( WindowPtr window, Boolean becomingActive )
  231. {
  232.     DrawGrowIcon( window );
  233. }
  234.  
  235.  
  236. /******************************** DoPicture *********/
  237.  
  238. void    DoPicture( WindowPtr window, PicHandle picture )
  239. {
  240.     Rect    drawingClipRect, myRect;
  241.     GrafPtr    oldPort;
  242.     RgnHandle    tempRgn;
  243.     
  244.     GetPort( &oldPort );
  245.     SetPort( window );
  246.     
  247.     tempRgn = NewRgn();
  248.     GetClip( tempRgn );
  249.     EraseRect( &window->portRect );
  250.     
  251.     DrawGrowIcon( window );
  252.  
  253.     drawingClipRect = window->portRect;
  254.     drawingClipRect.right -= kScrollBarAdjust;
  255.     drawingClipRect.bottom -= kScrollBarAdjust;
  256.     
  257.     myRect = window->portRect;
  258.     CenterPict( picture, &myRect );
  259.     ClipRect( &drawingClipRect );
  260.     DrawPicture( picture, &myRect );
  261.     
  262.     SetClip( tempRgn );
  263.     DisposeRgn( tempRgn );
  264.     SetPort( oldPort );
  265. }
  266.  
  267.  
  268. /****************** CenterPict ********************/
  269.  
  270. void    CenterPict( PicHandle picture, Rect *destRectPtr )
  271. {
  272.     Rect    windRect, pictRect;
  273.     
  274.     windRect = *destRectPtr;
  275.     pictRect = (**( picture )).picFrame;
  276.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  277.                            windRect.top     - pictRect.top);
  278.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  279.                           (windRect.bottom - pictRect.bottom)/2);
  280.     *destRectPtr = pictRect;
  281. }